package com.jingdong.dao.address.impl;


import com.jingdong.dao.address.CountyDao;
import com.jingdong.model.County;
import com.jingdong.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CountyDaoImpl implements CountyDao {
    public County getCountyById(int id) {
        try{
            SqlSession sqlSession= MyBatisUtils.getSqlSession();
            County county=sqlSession.selectOne("com.landeqing.model.countyModel.getCountyById",id);
            return county;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public List<County> getCountyList(int id) {
        try{
            SqlSession sqlSession=MyBatisUtils.getSqlSession();
            List<County> countyList=sqlSession.selectList("com.landeqing.model.countyModel.getCountyList",id);
            return  countyList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public int countySave(County county) {
        try{
            SqlSession sqlSession=MyBatisUtils.getSqlSession();
            int result=sqlSession.insert("com.landeqing.model.countyModel.countySave",county);
            sqlSession.commit();
            sqlSession.close();
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }
    //根据名字从县查询得到市的id
    public County getCountyForLikeByName(String stringCounty) {
        try{
            SqlSession sqlSession=MyBatisUtils.getSqlSession();

            List<County> list=  sqlSession.selectList("com.landeqing.model.countyModel.getCountyForLikeByName",stringCounty);
            if (list!=null&&list.size()>0){
                return list.get(0);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
